package com.manage.competition.service.impl;

import com.manage.competition.entity.Role;
import com.manage.competition.mapper.RoleMapper;
import com.manage.competition.repository.RoleRepository;
import com.manage.competition.service.IRoleService;
import com.manage.competition.util.SnowflakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Create with IDEA
 *
 * @Author: gitee.com/KamisamaXX
 * @Date: Create in 11:28 2019/3/24
 * @Description:
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl implements IRoleService {

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<String> findRole(Long userId){
        List<Role> list = roleRepository.findAllByUserId(userId);
        List<String> rolesCollection = new ArrayList<>();
        for (Role role:
             list) {
            rolesCollection.add(role.getRole());
        }
        return rolesCollection;
    }

    @Override
    public Role findUserRole(Long userId) {
        return roleRepository.findByUserId(userId);
    }

    @Override
    public int insert(Role role) {
        role.setId(SnowflakeUtil.nextId());
        return roleMapper.insert(role);
    }

    @Override
    public int update(Role role) {
        return roleMapper.updateByPrimaryKeySelective(role);
    }

}
